﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using MvcMovie.Models;

namespace MvcMovie.Middleware
{
    //基于工厂方法MiddlewareFactory 激活的中间件
    public class FactoryActivatedMiddleware : IMiddleware
    {
        private readonly MvcMovieContext _db;

        public FactoryActivatedMiddleware(MvcMovieContext db)
        {
            _db = db;
        }

        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            //业务逻辑处理
            //var keyValue = context.Request.Query["key"];

            //if (!string.IsNullOrWhiteSpace(keyValue))
            //{
            //    _db.Add(new Request()
            //    {
            //        DT = DateTime.UtcNow,
            //        MiddlewareActivation = "FactoryActivatedMiddleware",
            //        Value = keyValue
            //    });

            //    await _db.SaveChangesAsync();
            //}

            await next(context);
        }
    }
}
